<?php
/**
 *@file comment.php
 *@author Zhe.Zhou(zhouzhe@haibao.com)
 *@date 2015-8-30 下午6:39:07
 *@brief  
 */
namespace haibao\comment\common;

use haibao\frame\data\query\Condition;
use haibao\frame\data\query\Pager;
use haibao\cms\model\filter\ArticleImage as ArticleImageFitler;
use haibao\comment\model\data\Comments;
use haibao\frame\http\Request;
// use haibao\comment\Config;
class Comment{
    public $isReplyComment = false;
    public $commentContent = '';
	private static $maxLength = 0;
	private $pageTagArr = array();
	private static $keyData = array();
	private $matchTag = array();
	private $banWords = array();
	private static $responseCode = array(
	    '-2'=>'用户禁用',
	    '1'=>'评论成功',
	    '-1'=>'敏感词'
	);
    private $ip = 0;
    
	public function checkhtml($html){
		$html = preg_replace("/<script[^>]*>.*<\/script>/isU", '', $html);
		$html = preg_replace("/<a[^>]*>.*<\/a>/isU", '', $html);

		$html = nl2br(str_replace(array("\t", '   ', '  '), array('&nbsp; &nbsp; &nbsp; &nbsp; ', '&nbsp; &nbsp;', '&nbsp;&nbsp;'), $html));
		return addslashes($html);
	}

	/**
	 * 根据文本内容匹配屏蔽词
	 * @param 评论内容     $text
	 * @param 敏感词类型  $type 其中 0:全部敏感词；1：政治敏感词；2：普通敏感词；
	 */
	public function checkBanWord($text,$type = \haibao\comment\model\data\Comments::ALL_KEYWORDS ){
	    $this->ip = isset($_SERVER['HTTP_X_CNET_FORWARDED_FOR']) ? $_SERVER['HTTP_X_CNET_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
		$maxLength = 1;
		$politWords = $this->getPoliticeKey();
		$adWords = $this->getKey();
		if($type == 0){
		    $this->banWords = $politWords+$adWords;
		}elseif($type == 1){
	        $this->banWords = $politWords;
	    }elseif ($type == 2){
	        $this->banWords = $adWords;
	    }
		if(!empty($this->banWords)){
			foreach ( $this->banWords as $key => $value ) {
				self::$keyData [$value] = $key;
				$keyLength = strlen ( $value );
				if ($keyLength > $maxLength) {
					$maxLength = $keyLength;
				}
			}
		}else{
			$maxLength = self::$maxLength;
		}
		$this->dealText(addslashes($text), $maxLength);
		return $this->matchTag;
	}
	
	public function getCommentById($id){
		$commentFilter = new \haibao\comment\model\filter\Comments();
		$commentFilter->select(array(
				\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_COMMENT,
				\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_FLOOR_ID,
				\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_USER_ID,
				\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_USER_NICK,
				\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_COMMENT_IDS,
				\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_REPLY_COMMENT_ID,
				\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_USER_NICK,
		        \haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_UNREG_USER_ID,
		));
		$commentFilter->where(\haibao\comment\model\filter\Comments::CONDITION_FIELD_NAME_ID, Condition::CONDITION_EQUAL, $id);
		return \haibao\comment\business\Comments::getOne($commentFilter);
	}

	public function formatCommentDate($commentDate){
		$formatInfo = '';
		if(!is_string($commentDate)) $commentDate = $commentDate->format('Y-m-d H:i:s');
		$diffTime = time()-strtotime($commentDate);
		switch($diffTime){
			case $diffTime <=60:
				$formatInfo = '刚刚';
				break;
			case ($diffTime>60&&$diffTime<3600):
				if(round($diffTime/60) == 0){
					$formatInfo = '刚刚';
				}else{
					$formatInfo = round($diffTime/60).'分钟前';
				}
				break;
			case ($diffTime>=60&&$diffTime<86400):
				$formatInfo = round($diffTime/3600).'小时前';
				break;
			default:
				// 				$formatInfo = $comment->CreateTime->format('Y-m-d');
				$formatInfo = $commentDate;
				break;
		}
		return $formatInfo;
	}

	public function getUserAvatarById($uid, $size = 'middle', $type = '') {
		$size = in_array($size, array('big', 'middle', 'small')) ? $size : 'middle';
		$uid = abs(intval($uid));
		$uid = sprintf("%09d", $uid);
		$dir1 = substr($uid, 0, 3);
		$dir2 = substr($uid, 3, 2);
		$dir3 = substr($uid, 5, 2);
		$typeadd = $type == 'real' ? '_real' : '';
		$url='http://cdn3.hbimg.cn/store/uc_server/data/avatar/'.$dir1.'/'.$dir2.'/'.$dir3.'/'.substr($uid, -2).$typeadd."_avatar_$size.jpg";
		$isexist=$this->check_local_file_exists($url);

		return $isexist?$url:"http://bbs.haibao.com/uc_server/images/noavatar_{$size}.gif";
	}

	public function check_local_file_exists($url) {
		$path='/export/media_ext/'.substr(strstr($url,'/store/'),7);

		$found = false;
		if (@file_exists($path)) {
			$found = true;
		}
		return $found;
	}

	public function getChannelCodeByName($channel){
		$channnelArr =  array(
				'www'=>6,
				'fashion'=>310,
				'stars'=>1383,
				'beauty'=>24,
				'brands'=>955,
				'jewelrywatch'=>6863,
				'diy'=>520,
				'wedding'=>211,
				'accessory'=>7455,
				'fashionista'=>0,
				'designers'=>955,
				'star'=>1383,
				'brand'=>955,
				'life'=>520,
				'6'=>6,
				'310'=>310,
				'24'=>24,
				'6863'=>6863,
				'630'=>520,
				'211'=>211,
				'7455'=>7455,
				'0'=>0,
				'2848'=>955,
				'1383'=>1383,
				'955'=>955,
				'520'=>520,
				'0'=>0,
				'tv'=>11280,
		        '11280'=>11280
		);
		return $channnelArr[$channel];
	}

	public function replaceFaceImageUrl($content){
		$faceImgArr = $this->getFaceImageUrl();
		foreach($faceImgArr as $key=>$val){
			$img = '<img src="'.$val.'" alt="表情符号">';
			$content = str_replace('['.$key.']', $img, $content);
		}
		$content = str_replace("\n","<br/>",$content);
		
		return $content;
	}
	public function getArticleImage($articleId,$page){
		$articleImageFilter = new ArticleImageFitler;
		$articleImageFilter->select(array(
				ArticleImageFitler::CONDITION_FIELD_NAME_PIC_URL,
				ArticleImageFitler::CONDITION_FIELD_NAME_IMAGE_WIDTH,
				ArticleImageFitler::CONDITION_FIELD_NAME_IMAGE_HEIGHT,
				ArticleImageFitler::CONDITION_FIELD_NAME_IMAGE_ID
		));
		$articleImageFilter->where(ArticleImageFitler::CONDITION_FIELD_NAME_ARTICLE_ID, Condition::CONDITION_EQUAL, $articleId);
		$articleImageFilter->setPager(new Pager(12,$page));
		$articleImages = \haibao\cms\business\Article::getArticleList ( $articleImageFilter );
		$imageList = array();
		if($articleImages){
			foreach($articleImages as $image){
				$imageList[$image->ImageId]= \haibao\comment\business\Base::getImageLink($image->PicUrl,100,100,75,1);
			}
		}
		$hasMore = false;
		if ($articleImageFilter->getPager()->getPageCount() > $page-1){
			$hasMore = true;
		}
		return array($imageList,$hasMore);
	}

	// DO : 用户名隐藏处理
	// @param : $str 需要隐藏的字符串
	// @param : $cutLen 显示的字符串长度
	// @return : 返回隐藏的字符串
	public static function hideUserName($str,$cutLen = 1){
		$len = mb_strlen($str,"UTF8");
		if($len>3){
			return self::processStr($str,0,$cutLen).str_pad('',floor($len-$cutLen*2),'*').self::processStr($str,($len-$cutLen),$cutLen);
		}else{
			return self::processStr($str,0,$cutLen).'**';
		}
	}
	// DO : 处理用户名的隐藏功能（隐藏中间部分字符）
	public static function processStr($str, $from, $len){
		return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s','$1',$str);
	}


	public static function getObejectInfo($objectId,$commentType,$isPc=false){

		$currPos = array();
		$topTagId = 0;
		switch ($commentType){
			case \haibao\comment\model\data\CommentType::TYPE_CODE_ARTICLE:

				$articleFilter = new \haibao\cms\model\filter\Article();
				$articleFilter->select(array(
						\haibao\cms\model\filter\Article::CONDITION_FIELD_NAME_ID,
						\haibao\cms\model\filter\Article::CONDITION_FIELD_NAME_TITLE
				));

				$articleFilter->where(\haibao\cms\model\filter\Article::CONDITION_FIELD_NAME_ARTICLE_PUBLISHID,Condition::CONDITION_EQUAL,(int)$objectId);
				$articleInfo = \haibao\cms\business\Article::getArticleModel($articleFilter);
				if(!$articleInfo) \haibao\frame\http\Response::redirect('/error.html?code=404',404);
				if($isPc){
					$articlePrimaryTag = \haibao\cms\business\Article::getArticlePrimaryTag(array($articleInfo->Id));
					if(!isset($articlePrimaryTag[$articleInfo->Id])){
						$topTagId = 6;
						array_push($currPos,'文章评论列表页');
					}else{
						list($currPos,$topTagId)  = \haibao\comment\business\Base::getCurrentPos($articlePrimaryTag[$articleInfo->Id]);
						array_push($currPos,'文章评论列表页');
					}
					$url = \haibao\cms\business\control\Base::getArticleUrl($objectId,$topTagId);
				}else{
					$url = \haibao\comment\Config::WAP_HOST_URL.'/articlem/'.$objectId.'.htm';
				}
				$title = $articleInfo->Title;
				$siteType = \haibao\cms\business\control\Base::DATA_TYPE_ARTICLE;
				$infoId = $articleInfo->Id;
				break;

			case \haibao\comment\model\data\CommentType::TYPE_CODE_PIC:
				$picFilter = new \haibao\cms\model\filter\imagecollective\imagecollective();
				$picTagFilter = new \haibao\cms\model\filter\imagecollective\ImageCollectiveTag();
				$picFilter->select(array(
						\haibao\cms\model\filter\imagecollective\imagecollective::CONDITION_FIELD_NAME_ID,
						\haibao\cms\model\filter\imagecollective\imagecollective::CONDITION_FIELD_NAME_TITLE
				));
				$picTagFilter->select(array(
						\haibao\cms\model\filter\imagecollective\ImageCollectiveTag::CONDITION_FIELD_NAME_TAG_ID,
				));
				$picTagFilter->where(\haibao\cms\model\filter\imagecollective\ImageCollectiveTag::CONDITION_FIELD_NAME_TAG_ISPRIMAIY,Condition::CONDITION_EQUAL,\haibao\cms\model\data\imagecollective\ImageCollectiveTag::IS_PRIMAIY);
				$picFilter->where(\haibao\cms\model\filter\imagecollective\imagecollective::CONDITION_FIELD_NAME_ID,Condition::CONDITION_EQUAL,(int)$objectId);
				$picFilter->leftJoin($picTagFilter,null,\haibao\cms\model\filter\imagecollective\imagecollective::CONDITION_FIELD_NAME_ID,\haibao\cms\model\filter\imagecollective\ImageCollectiveTag::CONDITION_FIELD_NAME_TAG_COLLECTIVE_ID,\haibao\cms\model\filter\imagecollective\imagecollective::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_TAG,false);
				$picInfo = \haibao\cms\business\imagecollective\ImageCollective::getImageCollectiveList($picFilter);
				if(!$picInfo) \haibao\frame\http\Response::redirect('/error.html?code=404',404);

				if($isPc){
					list($currPos,$topTagId)  = \haibao\comment\business\Base::getCurrentPos($picInfo->ImageCollectiveTag->TagId);
					array_push($currPos,'图集评论列表页');
					$url = \haibao\cms\business\control\Base::getArticleUrl($objectId,$topTagId,\haibao\cms\business\control\Base::DATA_TYPE_IMAGE_SET);
				}else{
					$url = \haibao\comment\Config::WAP_HOST_URL.'/pics/'.$objectId.'.htm';
				}


				$title = $picInfo->Title;
				$siteType = \haibao\cms\business\control\Base::DATA_TYPE_IMAGE_SET;
				$infoId = $picInfo->Id;
				break;
			case \haibao\comment\model\data\CommentType::TYPE_CODE_FEATURE:
				$featureInfo = \haibao\cms\business\FeaturePublish::getFeatureById($objectId);
				if(!$featureInfo) \haibao\frame\http\Response::redirect('/error.html?code=404',404);

				if($isPc){
					array_push($currPos,array('Url'=>'http://exclusive.haibao.com','TagName'=>'独家'));
					array_push($currPos,array('Url'=>'http://exclusive.haibao.com/feature/','TagName'=>'专题'));
					array_push($currPos,'专题评论列表页');
				}
				$url = $featureInfo->Url;
				$title = $featureInfo->FeatureDefine->FeatureName;
				$siteType = \haibao\cms\business\control\Base::DATA_TYPE_FEATURE;
				$infoId = $featureInfo->Id;
				break;

			case \haibao\comment\model\data\CommentType::TYPE_CODE_VIDEO:
				$videoInfo = \haibao\cms\business\Video::getOneById((int)$objectId);
				if(!$videoInfo) \haibao\frame\http\Response::redirect('/error.html?code=404',404);

				if($isPc){
					array_push($currPos,array('Url'=>'http://exclusive.haibao.com','TagName'=>'独家'));
					array_push($currPos,array('Url'=>'http://exclusive.haibao.com/video/','TagName'=>'视频'));
					array_push($currPos,'视频评论列表页');
				}
				$url = \haibao\comment\Config::EXCLUSIVE_DOMAIN.'/play/'.$videoInfo->Id.'.htm';
				$title = $videoInfo->Title;
				$siteType = \haibao\cms\business\control\Base::DATA_TYPE_VIDEO;
				$infoId = $videoInfo->Id;
				break;
			default :
				return array();
				break;
		}
		return  array(
				'currPos' => $currPos,
				'channel' => $topTagId,
				'title' => $title,
				'url' => $url,
				'siteType' => $siteType,
				'infoId' => $infoId,
		);
	}

	public static  function  getObejectSumNew($objectId,$type,$articleId = 0){
	    
	       $praiseSum = $commentSum = $questionSum = 0;
	     
	       switch ($type){
	     
	           case \haibao\comment\model\data\CommentType::TYPE_CODE_ARTICLE:
	     
	               if($articleId == 0){
	                   $articleFilter = new \haibao\cms\model\filter\Article();
	                   $articlePropertyFilter = new \haibao\cms\model\filter\ArticleProperty();
	                   $articlePropertyFilter->select(array(
	                       \haibao\cms\model\filter\ArticleProperty::CONDITION_FIELD_NAME_ID,
	                       \haibao\cms\model\filter\ArticleProperty::CONDITION_FIELD_NAME_ARTICLE_ID,
	                       \haibao\cms\model\filter\ArticleProperty::CONDITION_FIELD_NAME_ARTICLE_COMMENT_NUM,
	                       \haibao\cms\model\filter\ArticleProperty::CONDITION_FIELD_NAME_COMMENT_PARISE_NUM,
	                       \haibao\cms\model\filter\ArticleProperty::CONDITION_FIELD_NAME_QUESTION_NUM,
	                       \haibao\cms\model\filter\ArticleProperty::CONDITION_FIELD_NAME_QUESTION_PARISE_NUM
	                   ));
	                   $articleFilter->select(array(
	                       \haibao\cms\model\filter\Article::CONDITION_FIELD_NAME_ID
	                   ));
	                   $articleFilter->where(\haibao\cms\model\filter\Article::CONDITION_FIELD_NAME_ARTICLE_PUBLISHID, Condition::CONDITION_EQUAL, (int)$objectId);
	                   $articleFilter->innerjoin($articlePropertyFilter,null,\haibao\cms\model\filter\Article::CONDITION_FIELD_NAME_ID,\haibao\cms\model\filter\ArticleProperty::CONDITION_FIELD_NAME_ARTICLE_ID,\haibao\cms\model\filter\Article::CONDITION_FIELD_NAME_ARTICLE_PROPERTY,false);
	                   
	                   $articleInfo =\haibao\cms\business\Article::getArticleModel($articleFilter);
	                   if($articleInfo){
	                       $commentSum = $articleInfo->ArticleProperty->CommentNum ;
	                       $questionSum = $articleInfo->ArticleProperty->QuestionNum ;
	                       $praiseSum = $articleInfo->ArticleProperty->CommentPariseNum + $articleInfo->ArticleProperty->QuestionPariseNum;
	                   }
	               }else{
    	               $articlePropertyFilter = new \haibao\cms\model\filter\ArticleProperty();
    	               $articlePropertyFilter->select(array(
    	                   \haibao\cms\model\filter\ArticleProperty::CONDITION_FIELD_NAME_ID,
    	                   \haibao\cms\model\filter\ArticleProperty::CONDITION_FIELD_NAME_ARTICLE_ID,
    	                   \haibao\cms\model\filter\ArticleProperty::CONDITION_FIELD_NAME_ARTICLE_COMMENT_NUM,
    	                   \haibao\cms\model\filter\ArticleProperty::CONDITION_FIELD_NAME_COMMENT_PARISE_NUM,
	                       \haibao\cms\model\filter\ArticleProperty::CONDITION_FIELD_NAME_QUESTION_NUM,
	                       \haibao\cms\model\filter\ArticleProperty::CONDITION_FIELD_NAME_QUESTION_PARISE_NUM
    	               ));
    	               $articlePropertyFilter->where(\haibao\cms\model\filter\ArticleProperty::CONDITION_FIELD_NAME_ARTICLE_ID, Condition::CONDITION_EQUAL, (int)$articleId);
    	               $articleProperty =\haibao\cms\business\ArticleProperty::getArticleCommentNum($articlePropertyFilter,true);
    	     
    	               if($articleProperty){
    	                   $commentSum = $articleProperty->CommentNum ;
    	                   $questionSum = $articleProperty->QuestionNum;
    	                   $praiseSum = $articleProperty->CommentPariseNum + $articleProperty->QuestionPariseNum;
    	               }
	               }
	               break;
	     
	           case \haibao\comment\model\data\CommentType::TYPE_CODE_FEATURE:
	     
	               $featurePublishFilter = new \haibao\cms\model\filter\FeaturePublish();
	               $featurePublishFilter->select(array(
	                   \haibao\cms\model\filter\FeaturePublish::CONDITION_FIELD_NAME_ID,
	                   \haibao\cms\model\filter\FeaturePublish::CONDITION_FIELD_NAME_COMMENT_COUNT,
	                   \haibao\cms\model\filter\FeaturePublish::CONDITION_FIELD_NAME_COMMENT_PARISE_NUM,
	                   \haibao\cms\model\filter\FeaturePublish::CONDITION_FIELD_NAME_QUESTION_COUNT,
	                   \haibao\cms\model\filter\FeaturePublish::CONDITION_FIELD_NAME_QUESTION_PARISE_COUNT
	               ));
	               $featurePublishFilter->where(\haibao\cms\model\filter\FeaturePublish::CONDITION_FIELD_NAME_ID,Condition::CONDITION_EQUAL,(int)$objectId);
	     
	               $featureProperty =\haibao\cms\business\featurePublish::getFeatureNum($featurePublishFilter,true);
	     
	               if($featureProperty){
	                   $commentSum = $featureProperty->CommentCount;
	                   $questionSum = $featureProperty->QuestionCount;
	                   $praiseSum = $featureProperty->CommentPariseNum + $featureProperty->QuestionPariseCount;
	               }
	     
	               break;
	     
	           case \haibao\comment\model\data\CommentType::TYPE_CODE_PIC:
	     
	               $imageCollectivePropertyFilter = new \haibao\cms\model\filter\imagecollective\ImageCollectiveProperty();
	               $imageCollectivePropertyFilter->select(array(
	                   \haibao\cms\model\filter\imagecollective\ImageCollectiveProperty::CONDITION_FIELD_NAME_ID,
	                   \haibao\cms\model\filter\imagecollective\ImageCollectiveProperty::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_ID,
	                   \haibao\cms\model\filter\imagecollective\ImageCollectiveProperty::CONDITION_FIELD_NAME_IMAGE_COMMENT_NUM,
	                   \haibao\cms\model\filter\imagecollective\ImageCollectiveProperty::CONDITION_FIELD_NAME_COMMENT_PARISE_NUM,
	                   \haibao\cms\model\filter\imagecollective\ImageCollectiveProperty::CONDITION_FIELD_NAME_QUESTION_NUM,
	                   \haibao\cms\model\filter\imagecollective\ImageCollectiveProperty::CONDITION_FIELD_NAME_QUESTION_PARISE_NUM
	               ));
	               $imageCollectivePropertyFilter->where(\haibao\cms\model\filter\imagecollective\ImageCollectiveProperty::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_ID,Condition::CONDITION_EQUAL,(int)$objectId);
	               $imageCollectiveProperty = \haibao\cms\business\imagecollective\ImageCollectiveProperty::getImageCollectiveCommentNum($imageCollectivePropertyFilter);
	    
	               if($imageCollectiveProperty){
	                   $commentSum = $imageCollectiveProperty->CommentNum;
	                   $questionSum = $imageCollectiveProperty->QuestionNum;
	                   $praiseSum = $imageCollectiveProperty->CommentPariseNum + $imageCollectiveProperty->QuestionPariseNum;
	               }
	     
	               break;
	     
	           case \haibao\comment\model\data\CommentType::TYPE_CODE_VIDEO:
	               
	               $videoPropertyFilter = new \haibao\cms\model\filter\VideoProperty();
	               $videoPropertyFilter->select(array(
	                   \haibao\cms\model\filter\VideoProperty::CONDITION_FIELD_NAME_ID,
	                   \haibao\cms\model\filter\VideoProperty::CONDITION_FIELD_NAME_VIDEOID,
	                   \haibao\cms\model\filter\VideoProperty::CONDITION_FIELD_NAME_COMMENTNUM,
	                   \haibao\cms\model\filter\VideoProperty::CONDITION_FIELD_NAME_COMMENT_PARISE_NUM,
	                   \haibao\cms\model\filter\VideoProperty::CONDITION_FIELD_NAME_QUESTION_NUM,
	                   \haibao\cms\model\filter\VideoProperty::CONDITION_FIELD_NAME_QUESTION_PARISE_NUM
	               ));
	    
	               $videoPropertyFilter->where(\haibao\cms\model\filter\VideoProperty::CONDITION_FIELD_NAME_VIDEOID,Condition::CONDITION_EQUAL,(int)$objectId);
	               $videoProperty =\haibao\cms\business\VideoProperty::getVideoCommentNum($videoPropertyFilter);
	     
	               if($videoProperty){
	                   $commentSum = $videoProperty->CommentNum ;
	                   $questionSum = $videoProperty->QuestionNum;
	                   $praiseSum = $videoProperty->CommentPariseNum + $videoProperty->QuestionPariseNum;
	               }
	               break;

				case \haibao\comment\model\data\CommentType::TYPE_CODE_HB_HOME_BLOG:
	               	
	               	$blogController = \haibao\cms\business\control\pc\channel\blog\BlogController::getBlogInstance();
	               	$blogInfo = $blogController->getBlogInfoByBlogId($objectId,false);
					if($blogInfo){
	               		$commentSum = $blogInfo['comments_count'];
	               		$questionSum = $blogInfo['question_count'];
	               		$praiseSum = $blogInfo['comments_parise_count'] + $blogInfo['question_parise_count'];
	               	}
	               	break;

               	case \haibao\comment\model\data\CommentType::TYPE_CODE_STAR_PK:
               		 
               		$starPkPropertyFilter = new \haibao\cms\model\filter\starpk\StarPkProperty();
	               	$starPkPropertyFilter->select(array(
	                   \haibao\cms\model\filter\starpk\StarPkProperty::CONDITION_FIELD_NAME_ID,
	                   \haibao\cms\model\filter\starpk\StarPkProperty::CONDITION_FIELD_NAME_STAR_PK_ID,
	                   \haibao\cms\model\filter\starpk\StarPkProperty::CONDITION_FIELD_NAME_COMMENT_NUM,
	                   \haibao\cms\model\filter\starpk\StarPkProperty::CONDITION_FIELD_NAME_COMMENT_PARISE_NUM,
	               	    \haibao\cms\model\filter\starpk\StarPkProperty::CONDITION_FIELD_NAME_QUESTION_NUM,
	               	    \haibao\cms\model\filter\starpk\StarPkProperty::CONDITION_FIELD_NAME_QUESTION_PARISE_NUM
	               	));
	    
	               $starPkPropertyFilter->where(\haibao\cms\model\filter\starpk\StarPkProperty::CONDITION_FIELD_NAME_STAR_PK_ID,Condition::CONDITION_EQUAL,(int)$objectId);
	               $starPkProperty =\haibao\cms\business\starpk\StarPkProperty::getStarPkNum($starPkPropertyFilter);
	     
	               if($starPkProperty){
	                   $commentSum = $starPkProperty->CommentNum;
	                   $questionSum = $starPkProperty->QuestionNum;
	                   $praiseSum = $starPkProperty->CommentPariseNum + $starPkProperty->QuestionPariseNum;
	               }
	               break;
	           case \haibao\comment\model\data\CommentType::TYPE_CODE_BRAND:
	           	   $brandStatistic = \haibao\cms\business\Brand::getBrandCommentNum((int)$objectId);
	           	   if($brandStatistic){
	           	   		$commentSum = $brandStatistic->CommentsNum;
	           	   		$questionSum = $brandStatistic->QuestionNum;
	           	   		$praiseSum = $brandStatistic->CommentPariseNum + $brandStatistic->QuestionPariseNum;
	           	   }
	           	   break;
	           case \haibao\comment\model\data\CommentType::TYPE_CODE_STAR:
	           	   $starFilter = new \haibao\cms\model\filter\CelebrityRiseCount();
	           	   $starFilter->select(array(
	           	   		\haibao\cms\model\filter\CelebrityRiseCount::CONDITION_FIELD_NAME_COMMENTS_COUNT,
	           	   		\haibao\cms\model\filter\CelebrityRiseCount::CONDITION_FIELD_NAME_COMMENT_PARISE_NUM,
	           	       \haibao\cms\model\filter\CelebrityRiseCount::CONDITION_FIELD_NAME_QUESTION_NUM,
	           	       \haibao\cms\model\filter\CelebrityRiseCount::CONDITION_FIELD_NAME_QUESTION_PARISE_NUM
	           	   ));
	           	   $starFilter->where(\haibao\cms\model\filter\CelebrityRiseCount::CONDITION_FIELD_NAME_CELEBRITY_ID, Condition::CONDITION_EQUAL, (int)$objectId);
	           	   $starStatistic = \haibao\cms\business\Celebrity::getOne($starFilter);
	           	   if($starStatistic){
		           	   	$commentSum = $starStatistic->CommentsCount;
		           	   	$questionSum = $starStatistic->QuestionNum;
		           	   	$praiseSum = $starStatistic->CommentPariseNum + $starStatistic->QuestionPariseNum;
	           	   }
	           	   break;
	           case \haibao\comment\model\data\CommentType::TYPE_CODE_DESIGNER:
	           	   	$designerFilter = new \haibao\cms\model\filter\designer\DesignerProperty();
	           	   	$designerFilter->select(array(
	           	   			\haibao\cms\model\filter\designer\DesignerProperty::CONDITION_FIELD_NAME_COMMENTNUM,
	           	   			\haibao\cms\model\filter\designer\DesignerProperty::CONDITION_FIELD_NAME_COMMENT_PARISE_NUM,
    	           	   	    \haibao\cms\model\filter\designer\DesignerProperty::CONDITION_FIELD_NAME_QUESTION_NUM,
           	   	           \haibao\cms\model\filter\designer\DesignerProperty::CONDITION_FIELD_NAME_QUESTION_PARISE_NUM
	           	   	));
	           	   	$designerFilter->where(\haibao\cms\model\filter\designer\DesignerProperty::CONDITION_FIELD_NAME_DESIGNER_ID, Condition::CONDITION_EQUAL, (int)$objectId);
	           	   	$designerStatistic = \haibao\cms\business\Designer::getDesignerByFilter($designerFilter,true);
	           	   	if($designerStatistic){
	           	   		$commentSum = $designerStatistic->CommentNum;
	           	   		$questionSum = $designerStatistic->QuestionNum;
	           	   		$praiseSum = $designerStatistic->CommentPariseNum + $designerStatistic->QuestionPariseNum;
	           	   	}
	           	   	break;
           	   	case \haibao\comment\model\data\CommentType::TYPE_CODE_MEIJU_INFO:
           	   		$americanFilter = new \haibao\cms\model\filter\americandrama\AmericanProperty();
           	   		$americanFilter->select(array(
           	   				\haibao\cms\model\filter\americandrama\AmericanProperty::CONDITION_FIELD_NAME_COMMENT_NUM,
           	   				\haibao\cms\model\filter\americandrama\AmericanProperty::CONDITION_FIELD_NAME_COMMENT_PARISE_NUM,
           	   		       \haibao\cms\model\filter\americandrama\AmericanProperty::CONDITION_FIELD_NAME_QUESTION_NUM,
           	   		       \haibao\cms\model\filter\americandrama\AmericanProperty::CONDITION_FIELD_NAME_QUESTION_PARISE_NUM
           	   		));
           	   		$americanFilter->where(\haibao\cms\model\filter\americandrama\AmericanProperty::CONDITION_FIELD_NAME_AMERICANID, Condition::CONDITION_EQUAL, (int)$objectId);
           	   		$americanStatistic = \haibao\cms\business\americandrama\AmericanDefine::getOneAmericanDefine($americanFilter);
           	   		if($americanStatistic){
           	   			$commentSum = $americanStatistic->CommentNum;
           	   			$questionSum = $americanStatistic->QuestionNum;
           	   			$praiseSum = $americanStatistic->CommentPariseNum + $americanStatistic->QuestionPariseNum;
           	   		}
           	   		break;
           	   	case \haibao\comment\model\data\CommentType::TYPE_CODE_IMAGE_LIBRARY:
           	   		$imagePropertyFilter = new \haibao\cms\model\filter\ImageProperty();
           	   		$imagePropertyFilter->select(array(
           	   				$imagePropertyFilter::CONDITION_FIELD_NAME_COMMENT_NUM,
           	   				$imagePropertyFilter::CONDITION_FIELD_NAME_COMMENT_PARISE_NUM,
           	   		        $imagePropertyFilter::CONDITION_FIELD_NAME_QUESTION_NUM,
           	   		        $imagePropertyFilter::CONDITION_FIELD_NAME_QUESTION_PARISE_NUM
           	   		));
           	   		$imagePropertyFilter->where($imagePropertyFilter::CONDITION_FIELD_NAME_IMAGEID,Condition::CONDITION_EQUAL,(int)$objectId);
           	   		
           	   		$imagePropertyModel = \haibao\cms\business\ImageProperty::getCommentNum($imagePropertyFilter);
           	   		if($imagePropertyModel){
           	   			$commentSum = $imagePropertyModel->CommentNum;
           	   			$questionSum = $imagePropertyModel->QuestionNum;
           	   			$praiseSum = $imagePropertyModel->CommentPariseNum + $imagePropertyModel->QuestionPariseNum;
           	   		}
           	   		break;
				case \haibao\comment\model\data\CommentType::TYPE_CODE_FASHION_WEAR:
           	   			$fashionWearPropertyFilter = new \haibao\cms\model\filter\fashionwear\FashionWearProperty();
           	   			$fashionWearPropertyFilter->select(array(
                            $fashionWearPropertyFilter::CONDITION_FIELD_NAME_COMMENTNUM,
                            $fashionWearPropertyFilter::CONDITION_FIELD_NAME_COMMENTPRAISENUM,
                            $fashionWearPropertyFilter::CONDITION_FIELD_NAME_QUESTIONNUM,
                            $fashionWearPropertyFilter::CONDITION_FIELD_NAME_QUESTION_PARISE_NUM
           	   			));
           	   			$fashionWearPropertyFilter->where($fashionWearPropertyFilter::CONDITION_FIELD_NAME_FASHIONWEARID,Condition::CONDITION_EQUAL,(int)$objectId);
           	   			 
           	   			$fashionWearPropertyModel = \haibao\cms\business\fashionwear\FashionWear::getCommentNum($fashionWearPropertyFilter);
           	   			if($fashionWearPropertyModel){
           	   				$commentSum = $fashionWearPropertyModel->CommentNum;
           	   				$questionSum = $fashionWearPropertyModel->QuestionNum;
           	   				$praiseSum = $fashionWearPropertyModel->CommentPraiseNum + $fashionWearPropertyModel->QuestionPariseNum;
           	   			}
           	   			break;
                case \haibao\comment\model\data\CommentType::TYPE_CODE_GOODS:
           	   			$dataArr = \haibao\cms\business\newproduct\Goods::getStatisticByGoodsId($objectId);
           	   			if($dataArr){
                            $commentSum = $dataArr['CommentNum'];
                            $questionSum = $dataArr['QuestionNum'];
                            $praiseSum = $dataArr['CommentPariseNum'] + $dataArr['QuestionPariseNum'];
                        }
           	   			break;
	       }
	    
	    $personSum =$praiseSum+$commentSum+$questionSum;
	    return array(
	        'personSum'=> $personSum,
	        'commentSum'=> $commentSum,
	        'questionSum'=>$questionSum
	    );
	}

	private function dealText($text, $matchLength) {
		$str = '';
		$articleContentLength = strlen ( $text );
		if (! $matchLength) {
			$str .= $text;
			return $str;
		}

		if ($articleContentLength < $matchLength) {
			$str .= $this->dealText ( $text, $matchLength - 1 );
			return $str;
		}

		$matched = false;
		for($i = 0; $i <= $articleContentLength - $matchLength; $i ++) {
			$part = substr ( $text, $i, $matchLength );
			if (isset ( self::$keyData [$part] )) {
				$this->matchTag[self::$keyData[$part]] = $part;
				$str .= $part;
				$str .= $this->dealText ( substr ( $text, ($i + $matchLength), ($articleContentLength - $i - $matchLength) ), $matchLength );
				break;
			}
		}

		if (! $matched) {
			$str .= $this->dealText ( $text, $matchLength - 1 );
		}
	}

	private function getKey() {
		$ret_key_row = \haibao\comment\cache\CommentCache::getCommentKeyWords(\haibao\comment\cache\CommentCache::COMMENT_AD_KEY_WORDS);
		return json_decode($ret_key_row,true);
	}

	private function getPoliticeKey(){
	    $ret_key_row = \haibao\comment\cache\CommentCache::getCommentKeyWords(\haibao\comment\cache\CommentCache::COMMENT_POLITICS_KEY_WORDS);
	    return json_decode($ret_key_row,true);
	}
	private function getFaceImageUrl(){
		$faceArr = array(
				'眨眼' => 'http://cdn0.haibao.cn/js/editor/emot/emot-01.gif',
				'吃太饱' => 'http://cdn0.haibao.cn/js/editor/emot/emot-02.gif',
				'擦汗' => 'http://cdn0.haibao.cn/js/editor/emot/emot-03.gif',
				'高明' => 'http://cdn0.haibao.cn/js/editor/emot/emot-04.gif',
				'挑逗' => 'http://cdn0.haibao.cn/js/editor/emot/emot-05.gif',
				'害羞' => 'http://cdn0.haibao.cn/js/editor/emot/emot-06.gif',
				'哼小曲' => 'http://cdn0.haibao.cn/js/editor/emot/emot-07.gif',
				'再见' => 'http://cdn0.haibao.cn/js/editor/emot/emot-08.gif',
				'晕' => 'http://cdn0.haibao.cn/js/editor/emot/emot-09.gif',
				'疑惑' => 'http://cdn0.haibao.cn/js/editor/emot/emot-10.gif',
				'挖鼻孔' => 'http://cdn0.haibao.cn/js/editor/emot/emot-11.gif',
				'推眼镜' => 'http://cdn0.haibao.cn/js/editor/emot/emot-12.gif',
				'打劫' => 'http://cdn0.haibao.cn/js/editor/emot/emot-13.gif',
				'大笑' => 'http://cdn0.haibao.cn/js/editor/emot/emot-14.gif',
				'大哭' => 'http://cdn0.haibao.cn/js/editor/emot/emot-15.gif',
				'大叫' => 'http://cdn0.haibao.cn/js/editor/emot/emot-16.gif',
				'流泪' => 'http://cdn0.haibao.cn/js/editor/emot/emot-17.gif',
				'耍赖' => 'http://cdn0.haibao.cn/js/editor/emot/emot-18.gif',
				'做鬼脸' => 'http://cdn0.haibao.cn/js/editor/emot/emot-19.gif',
				'被淋湿' => 'http://cdn0.haibao.cn/js/editor/emot/emot-20.gif',
				'吐' => 'http://cdn0.haibao.cn/js/editor/emot/emot-21.gif',
				'想心思' => 'http://cdn0.haibao.cn/js/editor/emot/emot-22.gif',
				'扭屁屁' => 'http://cdn0.haibao.cn/js/editor/emot/emot-23.gif',
				'抽烟' => 'http://cdn0.haibao.cn/js/editor/emot/emot-24.gif',
				'泪奔' => 'http://cdn0.haibao.cn/js/editor/emot/emot-25.gif',
				'爱意' => 'http://cdn0.haibao.cn/js/editor/emot/emot-26.gif',
				'泪眼' => 'http://cdn0.haibao.cn/js/editor/emot/emot-27.gif',
				'拿来' => 'http://cdn0.haibao.cn/js/editor/emot/emot-28.gif',
				'恭喜男' => 'http://cdn0.haibao.cn/js/editor/emot/emot-29.gif',
				'恭喜女' => 'http://cdn0.haibao.cn/js/editor/emot/emot-30.gif',
				'奸笑' => 'http://cdn0.haibao.cn/js/editor/emot/emot-31.gif',
				'空' => 'http://cdn0.haibao.cn/js/editor/emot/emot-32.gif',
				'装死' => 'http://cdn0.haibao.cn/js/editor/emot/emot-33.gif',
				'嗨' => 'http://cdn0.haibao.cn/js/editor/emot/emot-34.gif',
				'幸福' => 'http://cdn0.haibao.cn/js/editor/emot/emot-35.gif',
				'念经' => 'http://cdn0.haibao.cn/js/editor/emot/emot-36.gif',
				'见鬼' => 'http://cdn0.haibao.cn/js/editor/emot/emot-37.gif',
				'爽' => 'http://cdn0.haibao.cn/js/editor/emot/emot-38.gif',
				'黄牌' => 'http://cdn0.haibao.cn/js/editor/emot/emot-39.gif',
				'红牌' => 'http://cdn0.haibao.cn/js/editor/emot/emot-40.gif',
				'被欺负' => 'http://cdn0.haibao.cn/js/editor/emot/emot-41.gif',
				'耍酷' => 'http://cdn0.haibao.cn/js/editor/emot/emot-42.gif',
				'发麻' => 'http://cdn0.haibao.cn/js/editor/emot/emot-43.gif',
				'洗澡' => 'http://cdn0.haibao.cn/js/editor/emot/emot-44.gif',
				'网虫' => 'http://cdn0.haibao.cn/js/editor/emot/emot-45.gif',
				'睡觉' => 'http://cdn0.haibao.cn/js/editor/emot/emot-46.gif',
				'打瞌睡' => 'http://cdn0.haibao.cn/js/editor/emot/emot-47.gif',
				'发狂' => 'http://cdn0.haibao.cn/js/editor/emot/emot-48.gif',
				'孤独' => 'http://cdn0.haibao.cn/js/editor/emot/emot-49.gif',
				'面壁' => 'http://cdn0.haibao.cn/js/editor/emot/emot-50.gif',
				'打人' => 'http://cdn0.haibao.cn/js/editor/emot/emot-51.gif',
				'水草舞' => 'http://cdn0.haibao.cn/js/editor/emot/emot-52.gif',
				'微笑' => 'http://cdn0.haibao.cn/js/editor/emot/emot-53.gif',
				'骑车' => 'http://cdn0.haibao.cn/js/editor/emot/emot-54.gif',
				'伤心' => 'http://cdn0.haibao.cn/js/editor/emot/emot-55.gif',
				'装可爱' => 'http://cdn0.haibao.cn/js/editor/emot/emot-56.gif',
				'发火' => 'http://cdn0.haibao.cn/js/editor/emot/emot-57.gif',
				'挖墙脚' => 'http://cdn0.haibao.cn/js/editor/emot/emot-58.gif',
				'色' => 'http://cdn0.haibao.cn/js/editor/emot/emot-59.gif',
				'竖眉' => 'http://cdn0.haibao.cn/js/editor/emot/emot-60.gif',
				'汗' => 'http://cdn0.haibao.cn/js/editor/emot/emot-61.gif',
				'啊' => 'http://cdn0.haibao.cn/js/editor/emot/emot-62.gif',
				'冷' => 'http://cdn0.haibao.cn/js/editor/emot/emot-63.gif',
				'寒' => 'http://cdn0.haibao.cn/js/editor/emot/emot-64.gif',
				'没什么' => 'http://cdn0.haibao.cn/js/editor/emot/emot-65.gif',
				'看飞机' => 'http://cdn0.haibao.cn/js/editor/emot/emot-66.gif',
				'打飞' => 'http://cdn0.haibao.cn/js/editor/emot/emot-67.gif',
				'踢' => 'http://cdn0.haibao.cn/js/editor/emot/emot-68.gif',
				'吐血' => 'http://cdn0.haibao.cn/js/editor/emot/emot-69.gif',
				'跪求' => 'http://cdn0.haibao.cn/js/editor/emot/emot-70.gif',
				'鞭打' => 'http://cdn0.haibao.cn/js/editor/emot/emot-71.gif',
				'鼻血' => 'http://cdn0.haibao.cn/js/editor/emot/emot-72.gif',
				'乐奔' => 'http://cdn0.haibao.cn/js/editor/emot/emot-73.gif',
				'亲' => 'http://cdn0.haibao.cn/js/editor/emot/emot-74.gif',
				'拳击' => 'http://cdn0.haibao.cn/js/editor/emot/emot-75.gif',
				'偷偷过' => 'http://cdn0.haibao.cn/js/editor/emot/emot-76.gif',
				'欢呼' => 'http://cdn0.haibao.cn/js/editor/emot/emot-77.gif',
				'黑洞' => 'http://cdn0.haibao.cn/js/editor/emot/emot-78.gif',
				'听不见' => 'http://cdn0.haibao.cn/js/editor/emot/emot-79.gif',
				'无语' => 'http://cdn0.haibao.cn/js/editor/emot/emot-80.gif',
		);
		return $faceArr;
	}
	public function packCommentsModel($userInfo,$commentInfo){
	    $commentModel = new Comments();
	    if(empty($userInfo['_auth_nick_name'])){
	        $userInfo['_auth_nick_name'] = $userInfo['_auth_user_name'];
	    }
	    if (is_numeric($userInfo['_auth_user_id'])){
	        $commentModel->UserId = $userInfo ['_auth_user_id'];
	    }else {
	        $commentModel->UserId = 0;
	        $commentModel->UnregUserId =  $userInfo['_auth_user_id'];
	    }
	    $commentModel->UserNick = $userInfo['_auth_nick_name'];
	    $commentModel->ObjectId = $commentInfo['objectId'];
	    $commentModel->Status = Comments::STATUS_OK;
	    $commentModel->IsPublic = $commentInfo['isPublic'];
	    $commentModel->IsBack = $commentInfo['isBack'];
	    $commentModel->IsVerify = \haibao\comment\model\data\Comments::VERIFY_YES;
	    $commentModel->IsSensitive = \haibao\comment\model\data\Comments::ISSENSITIVE_NO;
	    $commentModel->ReplyCommentId = 0;
	    $commentModel->IP = $this->ip;
	    $commentModel->MyComment = $commentInfo['content'];
	    $commentModel->Source = $commentInfo['source'];
	    $commentModel->CommentsTypeCode = $commentInfo['type'];
	    $commentModel->Praise = 0;
	    $commentModel->Report = 0;
	    $commentModel->ReplyNum = 0;
	    $commentModel->CreateBy = $commentInfo['createBy'];
	    $commentModel->CreateTime = new \DateTime ();
	    $commentModel->Channel = 0;
	    if (isset($commentInfo['channel']) && $commentInfo['channel']) {
	        $commentModel->Channel = $this->getChannelCodeByName ( $commentInfo['channel'] );
	    }
	    $commentContentData = new \haibao\comment\model\data\CommentContent();
	    $commentContentData->ParentCommentId = $commentInfo['commentId'];
	    $commentContentData->Content = $this->replyCommentBaseInfo($userInfo, $commentInfo);
	    $commentContentData->UserId = $commentModel->UserId;
	    $commentContentData->IsQuestion = $commentInfo['commentContentType'];
	    $commentContentData->IsAdopt = \haibao\comment\model\data\CommentContent::ISADOPT_NO;
	    $commentContentData->CreateTime = new \DateTime();
	    $commentContentData->MainCommentStates = \haibao\comment\model\data\CommentContent::MAIN_COMMENT_STATES_ADOPT_NO;
	    $commentModel->CommentContent = $commentContentData;
	    if ($commentInfo['commentId'] == 0) {
	        $commentModel->FloorId = 1;
	        $commentModel->CommentIds = 0;
	        $commentModel->ReplyUserId = 0;
	        $arrData[] = $contents = $this->replyCommentBaseInfo($userInfo, $commentInfo);
	    } else {
	        list($arrData,$contents) = $this->dealCommentContent($commentModel);
	    }
	    $commentModel->Comment = json_encode ( $arrData );
	    $commentModel->CommentContent->Content = $contents;
	    
	    return $commentModel;
	}
	
	private function replyCommentBaseInfo($userInfo,$commentInfo){
	    return array(
	        'content' => $commentInfo['content'],
	        'userId' => $userInfo ['_auth_user_id'],
	        'userNick' => $userInfo ['_auth_nick_name'],
	        'commentId' => 0,
	        'isDel' => 0,
	        'time' => new \DateTime (),
	        'isVerify' => \haibao\comment\model\data\Comments::VERIFY_YES,
	        'isSensitive' => \haibao\comment\model\data\Comments::ISSENSITIVE_NO,
	        'replyUserId' => 0,
	        'replyUserNick' => 0
	    );
	}
	public function addNewComment($userInfo,$commentModel){
	    \haibao\comment\business\Comments::addComent ( $commentModel,$userInfo);
        if($commentModel->CommentContent->ParentCommentId){
            \haibao\comment\business\Comments::updateCommentReplyNum ( $commentModel->CommentContent->ParentCommentId );
        }
        if ($this->isReplyComment) {
            $comment = $this->getCommentById ( $commentModel->ReplyCommentId );
            $arrData = json_decode ( $comment->Comment, true );
            array_push ( $arrData, $this->commentContent );
        
            $key = count ( $arrData ) - 1;
            $arrData [$key] ['commentId'] = $commentModel->Id;
            \haibao\comment\business\Comments::updateCommentById ( $commentModel->ReplyCommentId, array (
                'Comment' => json_encode ( $arrData )
            ) );
        }
	    $this->dealCommentContentWithImage($commentModel); //处理评论内容包含图片
	    if($commentModel->IsVerify){
	        \haibao\comment\business\Comments::updateCommentNewNum($commentModel->ObjectId,$commentModel->UserId,$commentModel->CommentsTypeCode,$commentModel->CommentContent->IsQuestion,true,true);
	    }
	}
	private function dealCommentContent($commentModel){
	    $arrData = array();
    	$this->isReplyComment = true;
        $comment = $this->getCommentById ( $commentModel->CommentContent->ParentCommentId);
        if (empty ( $comment->ReplyCommentId )) {
            $commentModel->ReplyCommentId = $commentModel->CommentContent->ParentCommentId;
        } else {
            $commentModel->ReplyCommentId = $comment->ReplyCommentId;
            if ($comment->UserId == 0){
                $commentModel->CommentContent->Content['replyUserId'] = $comment->UnregUserId;
            }else {
                $commentModel->CommentContent->Content['replyUserId'] = $comment->UserId;
            }
            $commentModel->CommentContent->Content['replyUserNick'] = $comment->UserNick;
        }
        $commentModel->ReplyUserId = 0;
        if($comment->UserId){
        	$commentModel->ReplyUserId = $comment->UserId;
        }else{
        	if($comment->UnregUserId){
        		$commentModel->ReplyUserId = $comment->UnregUserId;
        	}
        }
        $commentModel->FloorId = $comment->FloorId + 1;
        if ($comment->CommentIds) {
            $commentModel->CommentIds = $comment->CommentIds . ',~' . $commentModel->CommentContent->ParentCommentId . '~';
        } else {
            $commentModel->CommentIds = '~' . $commentModel->CommentContent->ParentCommentId . '~';
        }
        $arrData = json_decode ( $comment->Comment, true );
        $this->commentContent = $commentModel->CommentContent->Content;
        array_push ( $arrData, $commentModel->CommentContent->Content );
        
	    return array($arrData,$commentModel->CommentContent->Content);
	}
	private function dealCommentContentWithImage($commentModel){
	    $content =  $commentModel->MyComment;
	    preg_match_all('/data-id=\"([0-9]+)\"/',$content,$matches);
	    if($matches && isset($matches[1])){
	        foreach($matches[1] as $imageId){
	            $content = preg_replace('/<IMG/s','<img',$content);
	            $content = preg_replace('/<img(.*?)>/s','',$content);
	            $newContent = str_replace('<br>', '', $content);
	            if(trim($newContent)){
	                $commentModel->CommentContent->Content = json_decode($commentModel->CommentContent->Content);
	                $commentModel->CommentContent->Content->content = $content;
	                $imageCommentModel = clone $commentModel;
	                $imageCommentModel->CommentsTypeCode = \haibao\comment\model\data\CommentType::TYPE_CODE_IMAGE_LIBRARY;
	                $imageCommentModel->ObjectId = $imageId;
	                $imageCommentModel->MyComment = $content;
	                if ($imageCommentModel->CommentContent->ParentCommentId == 0) {
	                    $arrData[] = $contents = $commentModel->CommentContent->Content;
	                } else {
	                    list($arrData,$contents) = $this->dealCommentContent($imageCommentModel);
	                }
	                $imageCommentModel->Comment = json_encode ( $arrData );
	                $imageCommentModel->CommentContent->Content = $contents;
	                \haibao\comment\business\Comments::addComent ( $imageCommentModel );
	                if($imageCommentModel->IsVerify){
	                    \haibao\comment\business\Comments::updateCommentNewNum($imageId,$commentModel->UserId,\haibao\comment\model\data\CommentType::TYPE_CODE_IMAGE_LIBRARY,$imageCommentModel->CommentContent->IsQuestion,true,true);
	                }
	            } else {
	                \haibao\cms\business\ImageProperty::updateCommentNum($imageId,true,$commentModel->CommentContent->IsQuestion);
	            }
	        }
	    }
	}
	
	public function checkCommentContent($userInfo,$content){
	    $responseCode = 1;
	    $this->ip = isset($_SERVER['HTTP_X_CNET_FORWARDED_FOR']) ? $_SERVER['HTTP_X_CNET_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
	    $content = htmlspecialchars_decode($content);
	    $content = strip_tags($content,'<img><br><p>');
	    $content = $this->replaceFaceImageUrl($content);
	    $content = preg_replace('/onload=\".*\"/isU', '', $content);
	    
	    $poliRes = $this->checkBanWord($content,\haibao\comment\model\data\Comments::POLITICS_KEYWORDS);
	    if(!empty($poliRes)) {
	        $responseCode = -1;
	        return array($content,$responseCode);
	    }
	    $res = $this->checkBanWord($content,\haibao\comment\model\data\Comments::AD_KEYWORDS);
	    if(!empty($res)) {
	        $responseCode = -1;
	        return array($content,$responseCode);
	    }
	    
	    if($this->checkUserIsBan($userInfo['_auth_user_id'],$this->ip)){
	        $responseCode = -2;
	        return array($content,$responseCode);
	    }
	    
	    return array($content,$responseCode);
	}
	public function packCommentModel($userInfo,$commentInfo,$isVerify=\haibao\comment\model\data\Comments::VERIFY_YES,$IsSensitive=\haibao\comment\model\data\Comments::ISSENSITIVE_NO){
	    $responseCode = 1;
	    $content =  $commentInfo['content'];
	    $commentId = $commentInfo['commentId'];
	    //$ipAddress = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
	    $ipAddress = isset($_SERVER['HTTP_X_CNET_FORWARDED_FOR']) ? $_SERVER['HTTP_X_CNET_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
	    
	    if($this->checkUserIsBan($userInfo['_auth_user_id'],$ipAddress)){
	        $responseCode = -2;
	        return $responseCode;
	    }
	    $commentModel = $this->packComment($userInfo, $ipAddress, $commentInfo,$isVerify,$IsSensitive);
	    $contents = array ();
	    $arrData = array ();
	    if ($commentInfo['commentId'] == 0) {
	        $commentModel->FloorId = 1;
	        $commentModel->CommentIds = 0;
	        $commentModel->ReplyUserId = 0;
	        $arrData = $this->packCommentContent($userInfo,$commentModel,$commentInfo,$isVerify,$IsSensitive);
	    } else {
	        $arrData = $this->packCommentContent($userInfo,$commentModel,$commentInfo,$isVerify,$IsSensitive);
	        \haibao\comment\business\Comments::updateCommentReplyNum ( $commentId );
	    }
	    $commentModel->Comment = json_encode ( $arrData );
	    \haibao\comment\business\Comments::addComent ( $commentModel,$userInfo );
	    if ($this->isReplyComment) {
	        $comment = $this->getCommentById ( $commentModel->ReplyCommentId );
	        $arrData = json_decode ( $comment->Comment, true );
	        array_push ( $arrData, $this->commentContent );

	        $key = count ( $arrData ) - 1;
	        $arrData [$key] ['commentId'] = $commentModel->Id;
	        \haibao\comment\business\Comments::updateCommentById ( $commentModel->ReplyCommentId, array (
	        		'Comment' => json_encode ( $arrData )
	        ) );
	    }
		preg_match_all('/data-id=\"([0-9]+)\"/',$content,$matches);
		if($matches && isset($matches[1])){
			foreach($matches[1] as $imageId){
				$content = preg_replace('/<IMG/s','<img',$content);
				$content = preg_replace('/<img(.*?)>/s','',$content);
				$newContent = str_replace('<br>', '', $content);
				if(trim($newContent)){
					$commentInfo['content'] = $content;
					$imageCommentModel = clone $commentModel;
					$imageCommentModel->CommentsTypeCode = \haibao\comment\model\data\CommentType::TYPE_CODE_IMAGE_LIBRARY;
					$imageCommentModel->ObjectId = $imageId;
					$imageCommentModel->MyComment = $content;
					$arrData = $this->packCommentContent($userInfo,$imageCommentModel,$commentInfo,$isVerify,$IsSensitive);
					$imageCommentModel->Comment = json_encode ( $arrData );
					
					$commentData = new \haibao\comment\data\Comments();
					$commentData->add($imageCommentModel);
					
					if($isVerify){
						#$this->addCommentNum($imageId,$commentModel->UserId,\haibao\comment\model\data\CommentType::TYPE_CODE_IMAGE_LIBRARY,$commentInfo['CommentContentType']);
                        $this->addCommentNum($imageId,$commentModel->UserId,\haibao\comment\model\data\CommentType::TYPE_CODE_IMAGE_LIBRARY);
					}
				} else {
					\haibao\cms\business\ImageProperty::updateCommentNum($imageId);
				}
			}
		}
	    if($isVerify){
	        $this->addCommentNum($commentModel->ObjectId,$commentModel->UserId,$commentInfo['type']);
	    }
	    return $responseCode;
	}
	 private function packArrmes($userInfo,$commentModel){
	    $arrMes = array('uid'=>$commentModel->ReplyUserId,'type'=>'system','new'=>1);
	    $url = $this->getCommentDetailUrl($commentModel->ObjectId,$commentModel->CommentsTypeCode);
	    $arrMes['note'] = $userInfo ['_auth_nick_name'].'&nbsp;回复了你的评论&nbsp;<a href="'.$url.'#iframeComment" target="_blank">点击查看</a>';
	    $arrMes['dateline'] = time();
	    $arrMes['from_idtype'] = 'sendnotice';
	    $arrMes['category'] = 3;
	    return $arrMes;
	}
	
	private function packComment($userInfo,$ipAddress,$commentInfo,$isVerify=\haibao\comment\model\data\Comments::VERIFY_YES,$IsSensitive=\haibao\comment\model\data\Comments::ISSENSITIVE_NO){
	    $commentModel = new Comments();
	    if(empty($userInfo['_auth_nick_name'])){
	        $userInfo['_auth_nick_name'] = $userInfo['_auth_user_name'];
	    }
	    if (is_numeric($userInfo['_auth_user_id'])){
	        $commentModel->UserId = $userInfo ['_auth_user_id'];
	    }else {
	        $commentModel->UserId = 0;
	        $commentModel->UnregUserId =  $userInfo['_auth_user_id'];
	    }
	    $commentModel->UserNick = $userInfo['_auth_nick_name'];
	    $commentModel->ObjectId = $commentInfo['objectId'];
	    $commentModel->Status = Comments::STATUS_OK;
	    $commentModel->IsPublic = $commentInfo['isPublic'];
	    $commentModel->IsBack = isset($commentInfo['isBack'])?$commentInfo['isBack']:\haibao\comment\model\data\Comments::BACK_NO;
	    $commentModel->IsVerify = $isVerify;
	    $commentModel->IsSensitive = $IsSensitive;
	    $commentModel->ReplyCommentId = 0;
	    $commentModel->IP = $ipAddress;
	    $commentModel->MyComment = $this->replaceFaceImageUrl($commentInfo['content']);
	    $commentModel->Source = $commentInfo['source'];
	    $commentModel->CommentsTypeCode = $commentInfo['type'];
	    $commentModel->Praise = 0;
	    $commentModel->Report = 0;
	    $commentModel->ReplyNum = 0;
	    $commentModel->CreateBy = isset($commentInfo['createBy'])?$commentInfo['createBy']:\haibao\comment\model\data\Comments::CREATEBY;
	    $commentModel->CreateTime = new \DateTime ();
	    $commentModel->Channel = 0;
	    if ($commentInfo['channel']) {
	        $commentModel->Channel = $this->getChannelCodeByName ( $commentInfo['channel'] );
	    }
	    return $commentModel;
	}
	
	private function packCommentContent($userInfo,$commentModel,$commentInfo,$isVerify,$isSensitive=\haibao\comment\model\data\Comments::ISSENSITIVE_NO){
	    //$contentData = $this->replaceFaceImageUrl($commentInfo['content']);
	    $contents ['content'] = $commentInfo['content'];
		$contents ['userId'] = $userInfo ['_auth_user_id'];
	    $contents ['userNick'] = $userInfo ['_auth_nick_name'];
	    $contents ['commentId'] = 0;
	    $contents ['isDel'] = 0;
	    $contents ['time'] = new \DateTime ();
	    $contents ['isVerify'] = $isVerify;
	    $contents ['isSensitive'] = $isSensitive;
	    $contents ['replyUserId'] = 0;
	    $contents ['replyUserNick'] = 0;
	    $arrData = array();
	    if($commentInfo['commentId'] == 0){
	        if($commentInfo['content']){
	            //$contents['content'] = $commentInfo['content'];
	        }
	        $arrData[] = $contents;
	    }else{
	    	$this->isReplyComment = true;
	        $comment = $this->getCommentById ( $commentInfo['commentId'] );
	        if (empty ( $comment->ReplyCommentId )) {
	            $commentModel->ReplyCommentId = $commentInfo['commentId'];
	        } else {
	            $commentModel->ReplyCommentId = $comment->ReplyCommentId;
	            if ($comment->UserId == 0){
	                $contents ['replyUserId'] = $comment->UnregUserId;
	            }else {
	                $contents ['replyUserId'] = $comment->UserId;
	            }
	            $contents ['replyUserNick'] = $comment->UserNick;
	        }
	        $commentModel->ReplyUserId = 0;
	        if($comment->UserId){
	        	$commentModel->ReplyUserId = $comment->UserId;
	        }else{
	        	if($comment->UnregUserId){
	        		$commentModel->ReplyUserId = $comment->UnregUserId;
	        	}
	        }
	        $commentModel->FloorId = $comment->FloorId + 1;
	        if ($comment->CommentIds) {
	            $commentModel->CommentIds = $comment->CommentIds . ',~' . $commentInfo['commentId'] . '~';
	        } else {
	            $commentModel->CommentIds = '~' . $commentInfo['commentId'] . '~';
	        }
	        $arrData = json_decode ( $comment->Comment, true );
	        $this->commentContent = $contents;
	        array_push ( $arrData, $contents );
	    }
	    return $arrData;
	}
	
	private function checkUserIsBan($userId,$ip){
	    return \haibao\comment\business\CommentBan::checkUserIsBan($userId,$ip);
	}
 	private function getCommentDetailUrl($objectId,$type){
	    if (class_exists('\haibao\comment\Config')){
	        $config = '\haibao\comment\Config';
	    }else if (class_exists('\haibao\www\Config')){
	        $config = '\haibao\www\Config';
	    }else {
	        $config = '\haibao\cms\Config';
	    }
	    switch ($type){
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_ARTICLE:
	            $url = $config::HAIBAO_URL.'/article/'.$objectId.'.htm';
	            break;
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_FEATURE:
	            $featureModel = \haibao\cms\business\FeaturePublish::getFeatureById($objectId);
	            $url = $featureModel->Url;
	            break;
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_PIC:
	            $url = $config::PIC_HOST_URL.'/pics/'.$objectId.'.html';
	            break;
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_VIDEO:
	            $url = $config::EXCLUSIVE_DOMAIN.'/play/'.$objectId.'.htm';
	            break;
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_STAR_PK:
	            $url = $config::STARS_HOST_URL.'/starpk/'.$objectId.'/';
	            break;
            case \haibao\comment\model\data\CommentType::TYPE_CODE_GOODS:
                $url = $config::BRANDS_HOST_URL.'/product/'.$objectId.'/';
                break;
	    }
	    return $url;
	}
	public static function addCommentNum($objectId,$userId,$type){
	    switch ($type){
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_ARTICLE:
	            $arrArticleId = \haibao\cms\business\Article::getArticleIdByPublishId(array($objectId));
	            $articleId = array_pop($arrArticleId);
	            \haibao\cms\business\Article::addArticleCommentNum($articleId,1);
	            break;
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_FEATURE:
	            \haibao\cms\business\FeaturePublish::updateFeatureCommentNum($objectId,1);
	            break;
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_PIC:
	            \haibao\cms\business\imagecollective\ImageCollective::updateImageCollectiveCommentNum($objectId,1);
	            break;
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_VIDEO:
	            \haibao\cms\business\Video::updateCommentNum($objectId,1);
	            break;
	        case \haibao\comment\model\data\CommentType::TYPE_CODE_HB_HOME_BLOG:
	            \haibao\cms\business\control\pc\channel\blog\BlogController::updateCommentNum($objectId,1);
	            break;
            case \haibao\comment\model\data\CommentType::TYPE_CODE_STAR_PK:
            	\haibao\cms\business\starpk\StarPkProperty::updateCommentNum($objectId,1,false);
            	break;
            case \haibao\comment\model\data\CommentType::TYPE_CODE_BRAND:
            	\haibao\cms\business\Brand::updateCommentNum($objectId,$userId,1,false);
            	break;
            case \haibao\comment\model\data\CommentType::TYPE_CODE_STAR:
            	\haibao\cms\business\Celebrity::updateCommentNum($objectId,$userId,1,false);
            	break;
            case \haibao\comment\model\data\CommentType::TYPE_CODE_DESIGNER:
            	\haibao\cms\business\Designer::updateCommentNum($objectId,$userId,1,false);
            	break;
			case \haibao\comment\model\data\CommentType::TYPE_CODE_MEIJU_INFO:
				\haibao\cms\business\americandrama\AmericanDefine::updateCommentNum($objectId,1,false);
				break;
			case \haibao\comment\model\data\CommentType::TYPE_CODE_IMAGE_LIBRARY:
				\haibao\cms\business\ImageProperty::updateCommentNum($objectId,true);
			     break;
	     	case \haibao\comment\model\data\CommentType::TYPE_CODE_FASHION_WEAR:
	     		\haibao\cms\business\fashionwear\FashionWear::updateCommentNum($objectId,1,true);
	     		break;
            case \haibao\comment\model\data\CommentType::TYPE_CODE_GOODS:
                \haibao\cms\business\newproduct\Goods::updateCommentNum($objectId,1,true);
                break;
	    }
	}
	
}